<?php
/**
 * Person table data gateway
 *
 * @category    IPB_CMS
 * @package     Application_Model
 * @subpackage  DbTable
 * @copyright   Copyright (c) 2009 IPB CMS <http://ipb-cms.googlecode.com/>
 * @license     http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License
 * @author      Henrique Moody <henriquemoody@gmail.com>
 */
class Model_DbTable_Person extends App_Model_DbTable_Abstract
{

    /**
     * Name of the database table
     *
     * @var string
     */
    protected $_name = 'person';

    /**
     * Returns a person by its ID
     *
     * @param integer $personId
     */
    public function getPerson($personId)
    {
        $row = $this->fetchRow('personId = ' . (integer) $personId);
        $data = array();
        if ($row) {
            $data = $row->toArray();
        }
        return $data;
    }

    /**
     * Records a new person in the database
     *
     * Sets the ID of the Model_Person Object with the ID of the
     * primary key of the row inserted.
     *
     * @param Model_Person $person Person to record
     * @return integer|null The primary key of the row inserted.
     */
    public function createPerson(Model_Person &$person)
    {
        $person->setCreated(time());
        $data = array(
            'personCreated' => $person->getCreated()->getIso(),
            'personFirstName' => $person->getFirstName(),
            'personLastName' => $person->getLastName(),
            'personType' => $person->getType()
        );
        $id  = $this->insert($data);
        if ($id) {
            $person->setId($id);
        }
        return $person->getId();
    }

}
